//
//  TSMsgSearchResultTableViewController.m
//  chart
//
//  Created by xu on 15/8/16.
//  Copyright (c) 2015年 xu. All rights reserved.
//

#import "TSMsgSearchResultTableViewController.h"
#import "TSMessageListCell.h"
#import "TSRecieverListModel.h"

@interface TSMsgSearchResultTableViewController ()

@end

@implementation TSMsgSearchResultTableViewController{
    NSArray *dataArray;
    TSReceiveMsgListModel *listModel;
    NSString *currentUrl;
    NSString *timeBegin;
    NSString *timeEnd;
    NSString *keyWords;
    NSString *sectionIds;
}
@synthesize type;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.frame = CGRectMake(0, NAV_AND_STATUS_BAR_HEIGHT, currentDeviceWidth, currentDeviceHeight-NAV_AND_STATUS_BAR_HEIGHT-TAB_BAR_HEIGHT);
    
    WS(weakSelf);
    listModel = [[TSReceiveMsgListModel alloc] init];
    
    listModel.requestParams = @{TSDictionaryKeyDefine_TimeBegin:@([TSTools convertDateFromString:timeBegin].timeIntervalSince1970)?:@"", TSDictionaryKeyDefine_TimeEnd:@([TSTools convertDateFromString:timeEnd].timeIntervalSince1970)?:@"", TSDictionaryKeyDefine_KeyWords:keyWords?:@"", TSDictionaryKeyDefine_PageIndex:@(0), TSDictionaryKeyDefine_PageSize:@(100), TSDictionaryKeyDefine_Section:sectionIds?:@""};
    [listModel fetchWithPath:currentUrl completionHandler:^(BOOL isSucceeded, NSError *error) {
        if (!weakSelf) {
            return;
        }
        
        __strong typeof (weakSelf)strongSelf = weakSelf;
        strongSelf->dataArray = strongSelf->listModel.list;
        [weakSelf.tableView reloadData];
    } NoProgress:NO];
}

- (void)setUrl:(NSString *)url StartDate:(NSString *)startTime EndTime:(NSString *)endTime Keywords:(NSString *)keywords SectionIds:(NSString *)sIds
{
    currentUrl = url;
    timeBegin = startTime;
    timeEnd = endTime;
    keyWords = keywords;
    sectionIds = sIds;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cell_identify = @"cellIdentify";
    TSMessageListCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_identify];

    if (indexPath.row == 0) {
         cell = [[TSMessageListCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cell_identify isHeader:true];
        return cell;
    }
    else if (!cell) {
        cell = [[TSMessageListCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cell_identify];
    }
    if (dataArray) {
        TSReceiveMsgModel *model = [dataArray objectAtIndex:indexPath.row-1];
        cell.isSendNotReceive = type;
        [cell setModel:model];
    }
    
    return cell;
}


@end
